import type { ICampItem, IControlInstance, IEntityInstance, IEntityObject } from "@@/apis/scn/type"
import type { DrawingTypeEnum } from "@@/constants/other-keys"
import type MeasuringTools from "@@/utils/MeasuringTools"
import type DrawEntityManager from "@@/utils/ScenarioEdit/DrawEntityManager"
/**
 * description: 想定编辑
 * fileName: scenario.ts
 * author: ssr
 * date: 2022-12-06 11:26:13
 * version: V1.0.0
 */
import { defineStore } from "pinia"

/**
 * 想定状态接口
 */
interface ScenarioState {
  // 已选实体显示
  selectedListShow: boolean
  // 实体列表显示
  entityListShow: boolean
  // 控制对象显示
  controlObjShow: boolean
  // 当前选中控制对象
  currentControlObj: { name: string, value: DrawingTypeEnum, type: string } | null
  // 当前选中实体对象
  currentEntityTemp: IEntityObject | null
  // 想定id
  scnId: string | null
  // 编辑实体
  drawEntityManager: DrawEntityManager | null
  // 记录实体实例
  selectedEntityList: IEntityInstance[]
  // 记录控制对象实例
  selectedObjList: IControlInstance[]
  // 当前所属方id
  currentSide: ICampItem | null
  // 测量工具
  measuringTool: MeasuringTools | null
}

/**
 * 想定状态管理
 */
export const useScenarioStore = defineStore("scenario", {
  state: (): ScenarioState => ({
    selectedListShow: true,
    entityListShow: true,
    controlObjShow: true,
    scnId: null,
    currentControlObj: null,
    currentEntityTemp: null,
    drawEntityManager: null,
    selectedEntityList: [],
    selectedObjList: [],
    currentSide: null,
    measuringTool: null
  }),

  getters: {
    /**
     * 获取想定ID
     */
    getScnId(): string | null {
      return this.scnId
    }
  },

  actions: {
    /**
     * 设置已选实体显示状态
     * @param flag 显示状态
     */
    setSelectedListShow(flag: boolean) {
      this.selectedListShow = flag
    },

    /**
     * 设置实体列表显示状态
     * @param flag 显示状态
     */
    setEntityListShow(flag: boolean) {
      this.entityListShow = flag
    },

    /**
     * 设置控制对象显示状态
     * @param flag 显示状态
     */
    setControlObjShow(flag: boolean) {
      this.controlObjShow = flag
    },

    /**
     * 设置当前选中实体对象
     * @param param 实体对象
     */
    setCurrentEntityTemp(param: IEntityObject | null) {
      this.currentEntityTemp = param
    },

    /**
     * 设置当前选中控制对象
     * @param obj 控制对象
     */
    setCurrentControlObj(obj: { name: string, value: DrawingTypeEnum, type: string } | null) {
      this.currentControlObj = obj
    },

    /**
     * 设置想定ID
     * @param id 想定ID
     */
    setScnId(id: string | null) {
      this.scnId = id
    },

    /**
     * 设置编辑实体
     * @param manager 编辑实体管理器
     */
    setDrawEntityManager(manager: DrawEntityManager | null) {
      this.drawEntityManager = manager
    },

    /**
     * 设置当前所属方
     * @param side 所属方
     */
    setCurrentSide(side: ICampItem | null) {
      this.currentSide = side
    },

    /**
     * 设置测量工具
     * @param tool 测量工具
     */
    setMeasuringTool(tool: MeasuringTools | null) {
      this.measuringTool = tool
    },

    /**
     * 添加实体实例
     * @param instance 实体实例
     */
    addEntityInstance(instance: IEntityInstance) {
      this.selectedEntityList.push(instance)
    },

    /**
     * 添加控制对象实例
     * @param instance 控制对象实例
     */
    addControlInstance(instance: IControlInstance) {
      this.selectedObjList.push(instance)
    },

    /**
     * 根据ID查找实体实例索引
     * @param id 实体ID
     * @returns 实体实例索引
     */
    getEntityInstanceById(id: string): number {
      return this.selectedEntityList.findIndex(item => item.id === id)
    },

    /**
     * 根据ID查找控制对象实例索引
     * @param id 控制对象ID
     * @returns 控制对象实例索引
     */
    getControlInstanceById(id: string): number {
      return this.selectedObjList.findIndex(item => item.id === id)
    },

    /**
     * 根据ID查找实体实例
     * @param id 实体ID
     * @returns 实体实例或undefined
     */
    findEntityInstanceById(id: string): IEntityInstance | undefined {
      return this.selectedEntityList.find(item => item.id === id)
    },

    /**
     * 根据ID查找控制对象实例
     * @param id 控制对象ID
     * @returns 控制对象实例或undefined
     */
    findControlInstanceById(id: string): IControlInstance | undefined {
      return this.selectedObjList.find(item => item.id === id)
    },

    /**
     * 根据ID删除实体实例
     * @param id 实体ID
     * @returns 是否删除成功
     */
    removeEntityInstanceById(id: string): boolean {
      const index = this.getEntityInstanceById(id)
      if (index !== -1) {
        this.selectedEntityList.splice(index, 1)
        return true
      }
      return false
    },

    /**
     * 根据ID删除控制对象实例
     * @param id 控制对象ID
     * @returns 是否删除成功
     */
    removeControlInstanceById(id: string): boolean {
      const index = this.getControlInstanceById(id)
      if (index !== -1) {
        this.selectedObjList.splice(index, 1)
        return true
      }
      return false
    },

    /**
     * 清空所有实体实例
     */
    clearEntityInstances() {
      this.selectedEntityList = []
    },

    /**
     * 清空所有控制对象实例
     */
    clearControlInstances() {
      this.selectedObjList = []
    },

    /**
     * 重置状态
     */
    resetState() {
      this.selectedListShow = true
      this.entityListShow = true
      this.controlObjShow = true
      this.scnId = null
      this.currentControlObj = null
      this.currentEntityTemp = null
      this.drawEntityManager = null
      this.selectedEntityList = []
      this.selectedObjList = []
      this.currentSide = null
      this.measuringTool = null
    }
  }
})

// 为了兼容旧代码，保留原来的导出方式
export const scenarioState = useScenarioStore
